﻿using Groove.Core.MediaFile;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Groove.Winform.Playlist
{
    public partial class UcPlaylist : UserControl
    {
        public IMediaFile SelectedMediaFile { get; private set; }

        public event EventHandler MediaFileSelected;

        public List<IMediaFile> Playlist { get; private set; }

        public int CurrentIndex { get; private set; }

        public int PreviousIndex { get; private set; }

        public int PlaylistLength { get; private set; }

        public UcPlaylist()
        {
            InitializeComponent();
            scrollBar.ScrollBarValueChanged += ScrollBar_ScrollBarValueChanged;
            MouseWheel += (s, e) =>
            {
                scrollBar.OnParentMouseWheel(e.Delta);
            };
        }

        private void ScrollBar_ScrollBarValueChanged(int scollbarValue)
        {
            pnlContainer.Top = -1 * scollbarValue;
        }

        public void SetPlaylist(List<IMediaFile> mediaFiles)
        {
            pnlContainer.Controls.Clear();
            Playlist = mediaFiles;
            SelectedMediaFile = null;
            PlaylistLength = mediaFiles.Count;
            for (var index = PlaylistLength - 1; index >= 0; index--)
            {
                var file = mediaFiles[index];
                var listItem = new UcPlaylistItem()
                {
                    Dock = DockStyle.Top,
                    Height = 60,
                    MediaFile = file,
                    Index = index
                };
                listItem.ControlClicked += ListItem_ControlClicked;
                pnlContainer.Controls.Add(listItem);
            }
            var totalHeight = mediaFiles.Count * 60;
            pnlContainer.Height = totalHeight;
            scrollBar.TotalHeight = totalHeight;
        }

        private void ListItem_ControlClicked(object sender, EventArgs e)
        {
            if (!(sender is UcPlaylistItem selectedItem)) return;
            SelectIndex(selectedItem.Index);            
        }

        public void SelectIndex(int index)
        {
            PreviousIndex = CurrentIndex;
            CurrentIndex = index;
            foreach (var ctl in pnlContainer.Controls)
            {
                if (!(ctl is UcPlaylistItem item)) continue;
                if (item.Index != index)
                {
                    item.SetPlaying(false);
                    continue;
                }
                SelectedMediaFile = item.MediaFile;
                item.SetPlaying(true);
            }
            MediaFileSelected?.Invoke(this, EventArgs.Empty);
        }

        public void SetPlayingFile(IMediaFile file)
        {
            foreach (var ctl in pnlContainer.Controls)
            {
                if (!(ctl is UcPlaylistItem item)) continue;
                PreviousIndex = CurrentIndex;
                CurrentIndex = item.Index;
                if(item.MediaFile.FullFileName == file.FullFileName)
                {
                    SelectedMediaFile = item.MediaFile;
                    item.SetPlaying(true);
                    continue;
                }
                item.SetPlaying(false);                
            }
        }
    }
}
